<template>
    <div class="container bg-gray">
        <div class="title title1">
            请输入新手机号
        </div>
        <div class="form-items">
            <div class="form-item border-b flex">
                <div class="field-icon">
                    <img src="../../assets/imgs/other/Smartphone@2x.png" alt="" style="width: 0.3867rem;height: 0.6267rem;">
                </div>
                <div class="flex-con">
                    <input v-model="formData.telephone" type="tel" placeholder="请输入手机号码">
                </div>
            </div>
             <div class="form-item flex">
                <div class="field-icon">
                    <img src="../../assets/imgs/other/valid-icon@2x.png" alt="" style="width: 0.48rem;height: 0.5733rem;">
                </div>
                <div class="flex-con">
                    <input v-model="formData.validcode" type="tel" placeholder="请输入验证码">
                </div>
                <div class="send-btn" @click="getcode">
                   {{isSending?this.leftSeconds+'秒后重发':'获取验证码'}}
                </div>
            </div>
        </div>
        <div class="btn-areas">
            <div class="btn-submit" @click="doSubmit">提交修改</div>
        </div>
       
    </div> 
</template>
<script>
export default {
  computed: {
    leftMinutes() {
      if (this.startTime > 0) {
        return this.nowTime - this.startTime;
      }
      return "";
    }
  },
  created() {
    this.startTime = new Date().getTime();
  },
  data() {
    return {
      formData: {},
      isSending: false,
      leftSeconds: 60
    };
  },
  methods: {
    
      getcode() {
      if (this.isSending) {
        return;
      }
      if (this.$assert("isEmpty", this.formData.telephone, "请输入手机号码")) {
        return;
      }
      var reg = /^1[0-9]{10}$/;
      if (!reg.test(this.formData.telephone)) {
        this.$toast("手机号码格式不正确");
        return;
      }
      this.$post("/api/user/get_check_code", {
        telephone: this.formData.telephone
      }).then(_ => {
        this.$toast("发送验证码成功");
      });
      this.isSending = true;
      this.startTime = new Date().getTime();
      setTimeout(this.tickCount.bind(this), 1000);
    },
    tickCount() {
      let passedTime = new Date().getTime() - this.startTime;
      let s = 60 - parseInt(passedTime / 1000);
      this.leftSeconds = s;
      if (s < 0) {
        this.isSending = false;
      } else {
        setTimeout(this.tickCount.bind(this), 1000);
      }
    },
    doSubmit() {
      if (this.$assert("isEmpty", this.formData.telephone, "请输入手机号码")) {
        return;
      }
      if (this.$assert("isEmpty", this.formData.validcode, "请输入验证码")) {
        return;
      }
      this.showLoading();
      this.$post("/api/changephone", this.formData)
        .then(el => {
          this.$toast('手机号码修改成功');
          this.$router.replace('/center');
          this.hideLoading();
        })
        .catch(_ => {
          this.hideLoading();
        });
    }
  }
};
</script>

<style lang="scss" scoped>
.btn-areas {
  padding-top: 0.7867rem;
  text-align: center;
  .btn-submit {
    display: inline-block;
    width: 8.4667rem;
    height: 1.0667rem;
    line-height: 1.0667rem;
    color: white;
    font-size: 0.4267rem;
    background-image: url("../../assets/imgs/other/btn.png");
    background-size: 100% 100%;
    background-position: left top;
  }
}
.send-btn {
  width: 2.4133rem;
  height: 0.72rem;
  line-height: 0.72rem;
  border-radius: 0.36rem;
  border: solid 1px #9f9f9f;
  font-size: 0.32rem;
  text-align: center;
  margin-left: 0.4rem;
}
.form-items {
  padding-top: 1rem;
  .form-item {
    padding: 0.3rem;
    background-color: #fff;
    input {
      width: 100%;
      border: none;
      outline: none;
    }
    .field-icon {
      width: 1.2rem;
      text-align: right;
      box-sizing: border-box;
      padding-right: 0.4rem;
    }
  }
}
.title {
  font-size: 0.42rem;
  text-align: center;
  .tip {
    padding-top: 0.5rem;
    font-size: 0.3rem;
    color: #333;
  }
}
.title1 {
  padding-top: 1rem;
}
.title2 {
  padding-top: 2rem;
}
</style>
